package com.haiyou.dc.api.controller;

import com.haiyou.common.util.R;
import com.haiyou.email.service.SpringEmailService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author root
 * @date 2020-01-04 18:37:46
 */
@RestController
@RequestMapping("/email")
@Slf4j
@Api(tags = "测试发送邮件")
@Validated
public class EmailController {

    @Autowired
    private SpringEmailService springEmailService;

    @ApiOperation(value = "发送验证码")
    @GetMapping("/sendCode")
    public R sendCode() {
        String xx="亲爱的userName ,感谢您注册现代国际市场研究项目交易系统。您的邮箱验证码是code，验证码将于30分钟后过期。";
        springEmailService.sendValidCode("1073140751@qq.com", "测试",xx);
        return R.ok();
    }

    @ApiOperation(value = "测试发送验证码")
    @GetMapping("/validSendCode")
    public R validSendCode(@RequestParam(value = "code") String code) {
        springEmailService.validEmailCode("1073140751@qq.com", code);
        return R.ok();
    }

    @ApiOperation(value = "发送邮件")
    @GetMapping("/send")
    public R info() {
        String[] receivers = {"1073140751@qq.com"};
        springEmailService.batchSendEmail(receivers, "测试", "<!doctype html>\n" +
                "<html lang=\"zh-cmn-Hans\">\n" +
                "<head>\n" +
                "    <meta charset=\"UTF-8\">\n" +
                "    <meta name=\"renderer\" content=\"webkit\"/>\n" +
                "    <meta http-equiv=\"X-UA-Compatible\" content=\"IE=edge,chrome=1\"/>\n" +
                "    <title>邮件通知模板</title>\n" +
                "\n" +
                "    <style type=\"text/css\">\n" +
                "        table {\n" +
                "            font-family: \"Trebuchet MS\", Arial, Helvetica, sans-serif;\n" +
                "            width: 100%;\n" +
                "            border-collapse: collapse;\n" +
                "        }\n" +
                "\n" +
                "        td, th {\n" +
                "            font-size: 1em;\n" +
                "            border: 1px solid #5B4A42;\n" +
                "            padding: 3px 7px 2px 7px;\n" +
                "        }\n" +
                "\n" +
                "        th {\n" +
                "            font-size: 1.1em;\n" +
                "            text-align: center;\n" +
                "            padding-top: 5px;\n" +
                "            padding-bottom: 4px;\n" +
                "            background-color: #24A9E1;\n" +
                "            color: #ffffff;\n" +
                "        }\n" +
                "    </style>\n" +
                "</head>\n" +
                "<body>\n" +
                "<h2>邮件消息通知</h2>\n" +
                "<table>\n" +
                "    <tr>\n" +
                "        <th>异常类</th>\n" +
                "        <th>异常消息</th>\n" +
                "        <th>异常原因</th>\n" +
                "    </tr>\n" +
                "    <tr>\n" +
                "        <td>${(exception.exceptionClass)!\"\"}</td>\n" +
                "        <td>${(exception.exceptionMessage)!\"\"}</td>\n" +
                "        <td>${(exception.exceptionCause)!\"\"}</td>\n" +
                "    </tr>\n" +
                "</table>\n" +
                "<br>\n" +
                "<b>########################################################################################################################</b>\n" +
                "<hr>\n" +
                "<b>########################################################################################################################</b>\n" +
                "<br>\n" +
                "<b>##############################具体异常信息如下：##############################</b>\n" +
                "<br>\n" +
                "<pre>${mail.content}</pre>\n" +
                "</body>\n" +
                "</html>\n");
        return R.ok();
    }

    @GetMapping("testExceptionSendEmail")
    public R testExceptionSendEmail() {
        int i = 1 / 0;
        return R.ok();
    }

}